package net.lahuobao.mall.controller.admin;

import net.lahuobao.mall.entity.admin.MallProductCategoryEntity;
import net.lahuobao.mall.entity.admin.MallProductCategorySpecEntity;
import net.lahuobao.mall.service.admin.MallProductCategoryServiceI;
import net.lahuobao.mall.service.admin.MallProductCategorySpecServiceI;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by clark on 2016/10/21 0021.
 * 商品类别规格
 */
@Controller
@RequestMapping("/mallProductCategoryController")
public class MallProductCategoryController  extends BaseController{


    @Autowired
    private MallProductCategoryServiceI categoryService;

    @Autowired
    private MallProductCategorySpecServiceI categorySpecService;

    @Autowired
    private MallProductCategoryServiceI mallProductCategoryService;


    @Autowired
    private SystemService systemService;
    /**
     * 获取主页面
     * @return
     */
    @RequestMapping(params = "index")
    public ModelAndView index(){
        return  new ModelAndView("net/lahuobao/mall/admin/mallProductCateIndex");
    }

    /**
     * 获取商品分类树
     */
    @RequestMapping(params = "tree")
    @ResponseBody
    public AjaxJson prductCateTree(){
        AjaxJson json = new AjaxJson();
        try {
            json.setObj(categoryService.getMallProductCategoryTree());
        } catch (Exception e) {
            json.setMsg("获取树失败!");
            e.printStackTrace();
        }finally {
            return json;
        }
    }

    //跳转到分类的规格值页面
    @RequestMapping(params = "specview")
    public ModelAndView getSpecView(String cateId){
        ModelAndView mv = new ModelAndView("net/lahuobao/mall/admin/mallProductSpecList");
        mv.addObject("cateid", cateId);
        return mv;
    }

    /**
     * 添加商品分类
     *
     * @param
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(MallProductCategoryEntity mallProductCategory, HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();
        //检查唯一性
        if(StringUtil.isNotEmpty(mallProductCategory.getCategoryname())){
            //判断该分类名称是否存在
            MallProductCategoryEntity query = new MallProductCategoryEntity();
            CriteriaQuery cq = new CriteriaQuery(MallProductCategoryEntity.class);
            query.setCategoryname(mallProductCategory.getCategoryname());
            query.setParentcategoryid(mallProductCategory.getParentcategoryid());
            HqlGenerateUtil.installHql(cq, query);
            List<MallProductCategoryEntity> cateLists = mallProductCategoryService.getListByCriteriaQuery(cq, false);
            if(cateLists.size() > 0){//存在重复值
                message = "该分类下已存在"+mallProductCategory.getCategoryname();
                j.setSuccess(false);
            }else{//不存在
                if (StringUtil.isNotEmpty(mallProductCategory.getId())) {
                    message = "商品分类更新成功";
                    MallProductCategoryEntity t = mallProductCategoryService.get(MallProductCategoryEntity.class, mallProductCategory.getId());
                    try {
                        MyBeanUtils.copyBeanNotNull2Bean(mallProductCategory, t);
                        mallProductCategoryService.saveOrUpdate(t);
                        systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
                    } catch (Exception e) {
                        e.printStackTrace();
                        message = "商品分类更新失败";
                    }
                } else {
                    message = "商品分类添加成功";
                    mallProductCategoryService.save(mallProductCategory);
                    systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
                }
            }
        }
        j.setMsg(message);
        return j;
    }

    /**
     * 删除商品分类
     * @param mallProductCategory
     * @param request
     * @return
     */

    @RequestMapping(params = "checkdel")
    @ResponseBody
    public AjaxJson chekdel(MallProductCategoryEntity mallProductCategory, HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();

        //判断该分类是否被引用
        MallProductCategorySpecEntity query = new MallProductCategorySpecEntity();
        CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecEntity.class);
        query.setCategoryid(mallProductCategory.getId());
        HqlGenerateUtil.installHql(cq, query);
        List<MallProductCategorySpecEntity> specLists = categorySpecService.getListByCriteriaQuery(cq, false);
        if(specLists.size() > 0){
            message = "该分类正在被引用不能删除！";
            j.setSuccess(false);
        }
        j.setMsg(message);
        return j;
    }

    /**
     * 删除
     * @param mallProductCategory
     * @param request
     * @return
     */
    @RequestMapping(params = "del")
    @ResponseBody
    public AjaxJson del(MallProductCategoryEntity mallProductCategory, HttpServletRequest request) {
        String message = null;
        AjaxJson j = new AjaxJson();
        mallProductCategory = systemService.getEntity(MallProductCategoryEntity.class, mallProductCategory.getId());
        message = "商品分类删除成功";
        mallProductCategoryService.delete(mallProductCategory);
        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
        j.setMsg(message);
        return j;
    }


    /**
     * 添加商品分类
     *
     * @param
     * @return
     */
    @RequestMapping(params = "upistop")
    @ResponseBody
    public AjaxJson upIstop(MallProductCategoryEntity mallProductCategory) {
        String message = null;
        AjaxJson j = new AjaxJson();
        if (StringUtil.isNotEmpty(mallProductCategory.getId())) {
            message = "更新成功！";
            MallProductCategoryEntity t = mallProductCategoryService.get(MallProductCategoryEntity.class, mallProductCategory.getId());
            try {
                MyBeanUtils.copyBeanNotNull2Bean(mallProductCategory, t);
                mallProductCategoryService.saveOrUpdate(t);
                systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
            } catch (Exception e) {
                e.printStackTrace();
                message = "更新失败！";
            }
        }
        j.setMsg(message);
        return j;
    }

}
